define(['./module','../services/auth'], function (controllers) {
  'use strict';
  controllers.controller('MainCtrl', ['$rootScope', '$scope', '$state', '$location', 'Auth',
    function ($rootScope, $scope, $state, $location, Auth) {
      $scope.user = Auth.user;
      $scope.userRoles = Auth.userRoles;
      $scope.accessLevels = Auth.accessLevels;
      $scope.rememberme = true;
      $scope.role = Auth.userRoles.user;
      $scope.userRoles = Auth.userRoles;

      $scope.logout = function () {
        $state.go('login');
        Auth.logout(function () {
          $state.go('login');
          //$location.path('/login');
        }, function () {
          $rootScope.error = "Failed to logout";
        });
      };
      $scope.login = function () {
        Auth.login({
            username: $scope.username,
            password: $scope.password,
            rememberme: $scope.rememberme
          },
          function (res) {
            $state.go('app.base.users.list');
          },
          function (err) {
            $rootScope.error = "Failed to login";
          });
      };
      $scope.loginOauth = function (provider) {
        $window.location.href = '/auth/' + provider;
      };
      $scope.register = function () {
        Auth.register({
            username: $scope.username,
            password: $scope.password,
            role: $scope.role
          },
          function () {
            $location.path('/');
          },
          function (err) {
            $rootScope.error = err;
          });
      };
    }]);
});

